package com.nut.api.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.common.Result;
import com.nut.entity.SysRole;
import com.nut.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping(value = "/list")
    public Result<IPage<SysRole>> list(@RequestParam(required = false) String name,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return sysRoleService.list(name, pageNo, pageSize);
    }

    @GetMapping(value = "/{id}")
    public Result<SysRole> detail(@PathVariable Integer id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    //获取当前角色所有菜单
    @GetMapping(value = "/menuIds")
    public Result<List<Integer>> menuIds(@RequestParam Integer id) {
        List<Integer> menuIds = sysRoleService.getMenuIdsById(id);
        return Result.ok(menuIds);
    }

    @PostMapping
    public Result add(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return Result.ok();
    }

    @PutMapping
    public Result update(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return Result.ok();
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        sysRoleService.deleteByIds(ids);
        return Result.ok();
    }

//    @PutMapping(value = "addRoleMenus")
//    public Result<?> addRoleMenus(@RequestBody SysRole sysRole) {
//        return sysRoleService.addRoleMenus(sysRole, sysRole.getMenuIds());
//    }

    @PutMapping(value = "updateRoleMenus")
    public Result<?> updateRoleMenus(@RequestBody SysRole sysRole) {
        return sysRoleService.updateRoleMenus(sysRole.getId(), sysRole.getMenuIds());
    }

}
